import { Injectable } from '@angular/core';
import { Observable, of, config } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { catchError, map, tap } from 'rxjs/operators';

import { ClsAjaxModel } from './models/cls-ajax-model';
// import { ClsCompanyModel } from './models/cls-company-model';


import { ClsConfig  } from './cls-config';
import { Options } from 'selenium-webdriver/firefox';

@Injectable({
  providedIn: 'root'
})

export class CompanyService {

  private config : ClsConfig;
  
  constructor(private http: HttpClient) 
  {
      this.config = new ClsConfig();
  }

  getCompanys(pageParam : object): Observable<ClsAjaxModel> {
      let url = this.config.getCompany;
      let data = new FormData();
      data.append('kind', pageParam["kind"]);
      data.append('page', pageParam["page"]);
      data.append('keywords', pageParam["keywords"]);
      return this.http.post<ClsAjaxModel>(url, data, {});

      // .pipe(
      //     map((x, i) => {
      //       console.log("aaa", x);
      //       console.log(i);
      //       return x;
      //     })
      //  )
  
  }
 
  /**
   * Handle Http operation that failed.
   * Let the app continue.
   * @param operation - name of the operation that failed
   * @param result - optional value to return as the observable result
   */
  private handleError<T> (operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {

      // TODO: send the error to remote logging infrastructure
      console.error(error); // log to console instead

      // TODO: better job of transforming error for user consumption
      //this.log(`${operation} failed: ${error.message}`);

      // Let the app keep running by returning an empty result.
      return of(result as T);
    };
  }
}
